//
//  main.c
//  Questions
//
//  Created by wangrui on 14-09-18.
//  Copyright (c) 2014年 PPS. All rights reserved.
//

#include <stdio.h>
#include <stdlib.h>
#include "cutils.h"

int values[50][50]; int boxFlags[50][50];
int n,m,p,q;
int minSteps = 99999999;
int next[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};


struct Node{
	int x;
	int y;
	int step;
	struct Node *father;
};

void printPath(struct Node *node)
{
	printf("end");
	while (node) {
		printf("<-(%d,%d)",node->x,node->y);
		node = node->father;
	}
	
	printf("\n");
}


/*
 * 广度遍历,遍历出的路径是最短的
 */

int main(int argc, const char * argv[])
{
	
	scanf("%d %d",&n,&m);
	
	for (int i=0; i<n; i++) {
		for (int j=0; j<m; j++) {
			scanf("%d",&values[i][j]);
		}
	}
	
	int startX,startY;
	scanf("%d %d %d %d",&startX,&startY,&p,&q);
	
	
	queue_t que = NULL;
	init_queue(&que);
	
	struct Node *first = (struct Node *)malloc(sizeof(struct Node));
	first->x = startX;
	first->y = startY;
	first->step = 0;
	first->father = NULL;
	
	boxFlags[startX][startY] = 1; //标记第一个点为已访问
	enqueue(que, first);
	
	int found = 0;
	while (!is_empty_queue(que)) {
		
		struct Node * head = (struct Node *)dequeue(que,NULL);
		
		for (int k=0; k<4; k++) { //上下左右
			
			int tx = head->x + next[k][0];
			int ty = head->y + next[k][1];
			
			if (tx > m-1 || tx < 0 || ty > n-1 || ty < 0) {
				continue;
			}
			
			if (boxFlags[tx][ty] == 0 && values[tx][ty] == 0) {//未走过且无障碍
				
				boxFlags[tx][ty] = 1;
		
				/*不能这样写
				 *这个是分配到堆栈上的内存, 括号忘了，作用域就结束了，下次进来分配的还是同一个地址
				 *
					struct Node new;
					new.x = tx;
					new.y = ty;
					new.step = head->step + 1;
					new.father = head;
				 *
				 */
				struct Node *new = (struct Node *)malloc(sizeof(struct Node));
				new->x = tx;
				new->y = ty;
				new->step = head->step + 1;
				new->father = head;
				enqueue(que, new);
			}
			
			if (tx == p && ty == q) {
				
				struct Node *noteThatFound = get_tail(que);
				
				if (noteThatFound->step < minSteps) {
					minSteps = noteThatFound->step;
				}
				
				found = 1;
				printPath(noteThatFound); //不能打印出所有的路径，因为一个元素被占领之后，之后的路径不能再通过这里
				
				break;
			}
		}
		
		if (found) {
			break;
		}
		
		//free(head); 因为要打印，所以不能释放
	}
	
	printf("\nmin steps:%d\n",minSteps);
	
	return 0;
}


